package com.lx.controller;

import com.lx.Exception.BusinessException;
import com.lx.dao.SysLogDao;
import com.lx.domain.mysyslog;
import com.lx.domain.users;
import com.lx.serv.impl.AdminServImpl;
import com.lx.serv.impl.UserServImpl;
import com.lx.utill.JwtUtill;
import com.lx.utill.TimeUtill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping(value = "/users")
public class UserController {
    @Autowired
    SysLogDao sysLogDao;
    //    private String password = "admin";
//    private String username = "admin";
    @Autowired
    private UserServImpl userServImpl;

    @Autowired
    private AdminServImpl adminServImpl;

    @GetMapping
    public Result getAll(@RequestHeader("token") String token) {
        if (!JwtUtill.VerifyToken(token)) {
            sysLogDao.insert(new mysyslog(TimeUtill.getTime(), "失效用户", "获取用户列表", "获取失败"));
            return new Result(Code.GET_FAIL, "获取失败", null);
        }
        String username = adminServImpl.getNameBytoken(token);
        if (!userServImpl.getAll().isEmpty()) {
            sysLogDao.insert(new mysyslog(TimeUtill.getTime(), username, "获取用户列表", "获取成功"));
            return new Result(Code.GET_SUCCESS, "获取成功", userServImpl.getAll());
        }
        sysLogDao.insert(new mysyslog(TimeUtill.getTime(), username, "获取用户列表", "获取失败"));
        return new Result(Code.GET_FAIL, "获取失败", null);
    }

    @PostMapping
    public Result addUser(@RequestBody users user, @RequestHeader("token") String token) {
        if (!JwtUtill.VerifyToken(token)) {
            sysLogDao.insert(new mysyslog(TimeUtill.getTime(), "失效用户", "添加用户", "添加失败"));
            return new Result(Code.ADD_FAIL, "token失效", null);
        }
        String adminName = adminServImpl.getNameBytoken(token);
        if (userServImpl.add(user)) {
            // serch name bytoken
            sysLogDao.insert(new mysyslog(TimeUtill.getTime(), adminName, "添加用户", "添加成功"));
            return new Result(Code.ADD_SUCCESS, "添加成功", null);
        }
        sysLogDao.insert(new mysyslog(TimeUtill.getTime(), adminName, "添加用户", "添加失败"));
        return new Result(Code.ADD_FAIL, "添加失败", null);
    }

    @PutMapping()
    public Result updateUser(@RequestBody users user, @RequestHeader("token") String token) {
        if (!JwtUtill.VerifyToken(token)) {
            sysLogDao.insert(new mysyslog(TimeUtill.getTime(), "失效用户", "修改用户", "修改失败"));
            return new Result(Code.UPDATE_FAIL, "token失效", null);
        }
        String adminName = adminServImpl.getNameBytoken(token);
        if (userServImpl.update(user)) {
            sysLogDao.insert(new mysyslog(TimeUtill.getTime(), adminName, "修改用户", "修改成功"));
            return new Result(Code.UPDATE_SUCCESS, "修改成功", null);
        }
        sysLogDao.insert(new mysyslog(TimeUtill.getTime(), adminName, "修改用户", "修改失败"));
        return new Result(Code.UPDATE_FAIL, "修改失败", null);
    }

    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable int id, @RequestHeader("token") String token) {
        if (id < 0) {
            throw new BusinessException(Code.DELETE_FAIL, "无效id: " + id);
        }
        if (!JwtUtill.VerifyToken(token)) {
            sysLogDao.insert(new mysyslog(TimeUtill.getTime(), "失效用户", "删除用户", "删除失败"));
            return new Result(Code.DELETE_FAIL, "token失效", null);
        }
        String adminName = adminServImpl.getNameBytoken(token);
        if (userServImpl.delete(id)) {
            sysLogDao.insert(new mysyslog(TimeUtill.getTime(), adminName, "删除用户", "删除成功"));
            return new Result(Code.DELETE_SUCCESS, "删除成功", null);
        }
        sysLogDao.insert(new mysyslog(TimeUtill.getTime(), adminName, "删除用户", "删除失败"));
        return new Result(Code.DELETE_FAIL, "删除失败", null);
    }

}
